<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>实现基于hash的跳转</title>
</head>

<body>
    <div id="app">
        <a href="#/news">新闻</a>
        <a href="#/home">首页</a>
        <!-- 路由的占位符，这个叫做动态路由。 -->
        <component :is="page"></component>
    </div>

    <script src="../Vue.js"></script>
    <script>
        // 定义子组件
        const news = {
            template: `<h1>我试试新闻业</h1>`
        }

        const home = {
            template: `<h1>我是首页呢</h1>`
        }

        let vm = new Vue({
            el: '#app',
            data() {
                return {
                    page: 'home'
                }
            },
            components: {
                news,
                home
            }
        })
        // 监听路由的hash变化
        window.onhashchange = function () {
            // 改变路由
            const path = location.hash.slice(2);
            vm.page = path;
        }
    </script>
</body>

</html>